<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\facade\Route;
use think\facade\View;
use think\facade\Log;
use dengje\workerman\process\timing\task\Test;
use \think\facade\Db;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

Route::get('think', function () {
    return 'hello,ThinkPHP6!';
});

Route::get('hello/:name', 'index/hello');
// 配置静态资源路由，将/assets路径重定向到/super/assets
Route::get('assets/:path', function (string $path) {
    $filename = public_path().'super/assets/' . $path;
    if (file_exists($filename)) {
        // 根据文件后缀设置正确的Content-Type
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        $contentType = 'application/octet-stream';
        
        // 常见文件类型的MIME类型映射
        $mimeTypes = [
            'css' => 'text/css',
            'js' => 'application/javascript',
            'png' => 'image/png',
            'jpg' => 'image/jpeg',
            'jpeg' => 'image/jpeg',
            'gif' => 'image/gif',
            'svg' => 'image/svg+xml',
            'woff' => 'application/font-woff',
            'woff2' => 'application/font-woff2',
            'ttf' => 'application/font-sfnt',
            'mp4' => 'video/mp4'
        ];
        
        if (isset($mimeTypes[strtolower($ext)])) {
            $contentType = $mimeTypes[strtolower($ext)];
        }
        
        // 直接读取文件内容并设置响应头
        header('Content-Type: ' . $contentType);
        header('Content-Length: ' . filesize($filename));
        header('Cache-Control: no-cache, no-store, must-revalidate');
        header('Pragma: no-cache');
        header('Expires: 0');
    $version = 'v=' . md5_file($filename);
    header('Cache-Control: no-cache, max-age=0, must-revalidate');
    if (strpos($_SERVER['REQUEST_URI'], '?') === false) {
        header('Location: ' . $_SERVER['REQUEST_URI'] . '?v=' . md5_file($filename));
        exit;
    }
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($filename)) . ' GMT');
        readfile($filename);
        exit;
    }
    return abort(404);
})->pattern(['path' => '.*\.\w+$']);
// 科普文章API路由
// Route::group('api/science', function() {
//     // 获取文章列表
//     Route::get('lists', 'api/Science/lists');
//     // 获取文章详情
//     Route::get('detail', 'api/Science/detail');
//     // 获取分类列表
//     Route::get('categories', 'api/Science/categories');
//     // 点赞文章
//     Route::post('like', 'api/Science/like');
//     // 搜索文章
//     Route::get('search', 'api/Science/search');
//     // 获取热门文章
//     Route::get('hot', 'api/Science/hot');
// });

// 科普文章列表页面路由已移至文件末尾

// 为super路径添加路由
Route::get('super', function() {
    $filename = public_path().'super/index.html';
    if (file_exists($filename)) {
        return file_get_contents($filename);
    }
    return abort(404);
});

// 为super下的文件添加路由支持
Route::get('super/:path', function (string $path) {
    $filename = public_path().'super/' . $path;
    if (file_exists($filename)) {
        // 根据文件后缀设置正确的Content-Type
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        $contentType = 'application/octet-stream';
        
        // 常见文件类型的MIME类型映射
        $mimeTypes = [
            'html' => 'text/html',
            'css' => 'text/css',
            'js' => 'application/javascript',
            'png' => 'image/png',
            'jpg' => 'image/jpeg',
            'jpeg' => 'image/jpeg',
            'gif' => 'image/gif',
            'svg' => 'image/svg+xml',
            'ico' => 'image/x-icon',
            'woff' => 'application/font-woff',
            'woff2' => 'application/font-woff2',
            'ttf' => 'application/font-sfnt',
            'mp4' => 'video/mp4'
        ];
        
        if (isset($mimeTypes[strtolower($ext)])) {
            $contentType = $mimeTypes[strtolower($ext)];
        }
        
        // 直接读取文件内容并设置响应头
        header('Content-Type: ' . $contentType);
    header('Content-Length: ' . filesize($filename));
    header('Cache-Control: public, max-age=300, must-revalidate');
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($filename)) . ' GMT');
        readfile($filename);
        exit;
    }
    return abort(404);
})->pattern(['path' => '.*']);

Route::get('test', function () {
   
//       $a =  exec("cd ".root_path()." &&  php think workerman status",$out);
//   var_dump($out);
  
//   $a =  \think\facade\Console::call('workerman',['reload']);
//   var_dump($a);

// \think\facade\Db::execute('set session WAIT_TIMEOUT=2;');
// $db = new \PDO('mysql:host=127.0.0.1;dbname=cos;charset=utf8','root','root');
// $db->exec('set session WAIT_TIMEOUT=2;');
// sleep(2);
// $db->exec('BEGIN;');
// \think\facade\Db::execute('BEGIN;');
// var_dump(2);
    // (new \dengje\workerman\process\queue\Client())->push('dengje\workerman\process\queue\task\Test',[199882],5);
    // (new \dengje\workerman\process\queue\Client())->push('dengje\workerman\process\queue\task\Job2',[199882]);
    // \think\facade\Queue::push('\abc\a99', $data = '', $queue = 'ooooo');
    // \think\facade\Queue::push('\abc\a2', $data = '', $queue = null);
    // \think\facade\Queue::later(100,'\abc8\9a', $data = '', $queue = 'jjjj');
    // (new \dengje\workerman\process\queue\Client3())->subscribe('jdjd','\a\c');
    // app(\dengje\workerman\process\queue\Client3::class)->send(
    //     $queue = '22jdjd', // 队列名
    //     $data = [], // 任务数据
    //     $delay = 10, // 延迟队列时长，单位：秒
    //     $maxRetry = null, // 最大重试次数（默认3次）
    //     $retryInterval = null // 每次重试间隔秒数（默认5秒）
    //   );
    
    // (new \dengje\workerman\process\queue\Client())->push('dengje\workerman\process\queue\task\Job1',[199882]);
    // (new \dengje\workerman\process\queue\Client())->push('dengje\workerman\process\queue\task\Job2',[12]);

    // return \dengje\workerman\Test::aa();
//     $client = new \dengje\workerman\process\queue\Client();

//     $client->subscribe([
//         'test' => Test::class
//     ]);
//     // copy_directory(root_path().'plugin/test/database/migrations/',root_path().'database/migrations/');
//     // $output = \think\facade\Console::call('migrate:run',[]);
//     // var_dump($output);
//     // $client->run();
//   (new  \dengje\workerman\process\queue\Client())->send(
// 			$queue = 'test', // 队列名
// 			$data = [], // 任务数据
// 			$delay = 10, // 延迟队列时长，单位：秒
// 			$maxRetry = null, // 最大重试次数（默认3次）
// 			$retryInterval = null // 每次重试间隔秒数（默认5秒）
// 		  );
   
       

});

// 科普文章列表页面路由
Route::get('science/list/index', function() {
    return view('admin@science/list');
});

// 科普文章列表页面路由 - 已注释，前端Vue路由将处理此路径
// Route::get('science/category/index', function() {
//     return view('admin@science/list');
// });



// 科普文章列表API接口
Route::get('science/api/list', 'app\admin\controller\science\Science@list');

// 科普文章分类列表API接口
Route::get('science/category/list', 'app\admin\controller\science\Category@list');
Route::get('science/api/categories', 'app\admin\controller\science\Category@select');

// 科普文章分类创建API接口
Route::post('science/api/category/create', 'app\admin\controller\science\Category@create');

// 科普文章分类更新API接口
Route::post('science/api/category/update', 'app\admin\controller\science\Category@update');





// 科普文章分类列表API接口
Route::get('science/report/list', 'app\admin\controller\science\Report@list');


// 科普文章分类创建API接口
Route::post('science/api/report/create', 'app\admin\controller\science\Report@create');

// 科普文章分类更新API接口
Route::post('science/api/report/update', 'app\admin\controller\science\Report@save');

Route::get('science/report/create', function() {
    return view('admin@science/report/create');
});

Route::get('science/feedback/create', function() {
    return view('admin@science/feedback/create');
});
///science/report/index

// 科普文章添加/编辑页面路由
Route::get('science/edit/index', function() {
    return view('admin@science/edit');
});

// 科普文章分类添加/编辑页面路由
Route::get('science/category/edit', function() {
    return view('admin@science/category_edit');
});

// 科普文章详情API接口
Route::get('science/api/detail', 'app\admin\controller\science\Science@detail');

// 科普文章创建API接口
Route::post('science/api/create', 'app\admin\controller\science\Science@create');

// 科普文章更新API接口
Route::post('science/api/update', 'app\admin\controller\science\Science@update');

// 图片上传接口
Route::post('file/image', 'app\admin\controller\science\Science@image');


// 用户选择API接口
Route::get('user/api/select', 'app\admin\controller\science\Science@selects');
// 用户详情API接口
Route::get('user/api/detail', 'app\admin\controller\science\Science@details');



// 领域分类列表PI接口
Route::get('science/cate/list', 'app\admin\controller\science\Cate@list');

// 领域分类下拉列表
Route::get('science/cate/lists', 'app\admin\controller\science\Cate@lists');

// 领域分类编辑页面
Route::get('science/cate/edit', function() {
    return view('admin@science/cate/create');
});
// 领域分类详情API接口
Route::get('science/cate/detail', 'app\admin\controller\science\Cate@detail');

// 领域分类创建API接口
Route::post('science/cate/add', 'app\admin\controller\science\Cate@add');

// 领域分类修改API接口
Route::post('science/cate/update', 'app\admin\controller\science\Cate@update');


//项目编辑 api 接口
Route::get('science/project/edit', function() {
    return view('admin@science/project/create');
});

// 项目列表PI接口
Route::get('science/project/list', 'app\admin\controller\science\Project@list');
// 项目创建API接口
Route::post('science/project/add', 'app\admin\controller\science\Project@add');

// 项目修改API接口
Route::post('science/project/update', 'app\admin\controller\science\Project@update');

// 项目删除API接口
Route::post('science/project/delete', 'app\admin\controller\science\Project@delete');


// 项目详情API接口
Route::get('science/project/detail', 'app\admin\controller\science\Project@detail');


// 认证列表页面
Route::get('science/authentication/index', 'app\admin\controller\science\Authentication@index');

// 认证创建/编辑页面
Route::get('science/authentication/create', 'app\admin\controller\science\Authentication@create');

// 认证列表API接口
Route::get('science/authentication/list', 'app\admin\controller\science\Authentication@list');

// 认证详情API接口
Route::get('science/authentication/detail', 'app\admin\controller\science\Authentication@detail');

// 认证创建API接口
Route::post('science/authentication/add', 'app\admin\controller\science\Authentication@add');

// 认证更新API接口
Route::post('science/authentication/update', 'app\admin\controller\science\Authentication@update');

// 认证删除API接口
Route::post('science/authentication/delete', 'app\admin\controller\science\Authentication@delete');

//上传图片接口
Route::post('file/upload/image', 'app\admin\controller\file\Upload@images');




//用户列表API接口
Route::get('admin/user.user/lists', 'app\admin\controller\user\User@lists');



